;(function($){
	function ScrollPagination(ele, options, func){
		this.ele = ele;
		this.options = options;
		this.init(this.ele, this.options, func);
	}
	ScrollPagination.prototype = {
		constructor: ScrollPagination,
		init: function(obj, opts, func){
			var that = this, target = opts.scrollTarget;
			$(obj).attr('scrollPagination', 'enabled');
			var timer = null;
			 $(target).scroll(function(event){
			 	timer && clearTimeout(timer);
			 	timer = setTimeout(function(){
			 		if ($(obj).attr('scrollPagination') == 'enabled'){
			 			that.loadContent(obj, opts, func);		
					}
					else {
						event.stopPropagation();	
					}
			 	}, 1000);
			 });
			 
			 that.loadContent(obj, opts, func, true);
		},
		loadContent: function(obj, opts, func, isload){
			if($(obj).attr("isLoad") == "true") return;
			var that = this, target = opts.scrollTarget;
			var mayLoadContent = opts.currentPage<=opts.totalPage && (isload || $(target).scrollTop() + $(target).height() - opts.heightOffset >= $(obj).height());
			if (mayLoadContent) {
				that.loading(target, true);
				if (opts.beforeLoad != null) {
					opts.beforeLoad();
				}
				var aPageAttr = ["currentPage","pageNum"]
				for(var i=0; i<aPageAttr.length; i++){
					var sAttr = aPageAttr[i];
					opts.requestData[sAttr] = opts[sAttr];
				}
				$.ajax({
					type: opts.type,
					url: opts.url,
					data: opts.requestData,
					success: function(data) {
						opts.analysisData(data);
						func && func(opts.responseData.list, opts.requestData);
						opts.currentPage++;	//页码自增				
						if (opts.afterLoad != null) {
							opts.afterLoad(data, true);
						}
						that.loading(target, false);
					},
					error: function(e) {
						opts.responseData = null;
						func && func(null);
						if (opts.afterLoad != null) {
							opts.afterLoad(e, false);
						}
						that.loading(target, false);
					},
					dataType: opts.dataType
				});
			}
		},
		loading: function(obj, b){
			if(b){	//正在加载
				$(obj).attr("isLoad", true);
//                    .append("<div class='loading'></div>");
			}else{
				$(obj).attr("isLoad", false).find(".loading").remove();
			}
		}
	};

	$.fn.scrollPagination = function(options, func){
		var opts = $.extend({}, $.fn.scrollPagination.defaults, options);
		var target = opts.scrollTarget;
		if (target == null){
			target = $(window); 
	 	}
		opts.scrollTarget = $(target);
		return this.each(function(index, ele) {
			new ScrollPagination($(ele), opts, func);
		});
	};
	$.fn.scrollPagination.defaults = {
		currentPage: 1,	//当前页码
		totalPage: 1,	//总页数
		pageNum: 20,	//每页显示数据
		analysisData: _analysisData,	//数据解析
		beforeLoad: $.noop,	//加载前调用
		afterLoad: $.noop,	//加载后调用
		scrollTarget: null, 	//滚动事件的对象
		heightOffset: 0,	//表格下边距（影响滚动到什么位置开始调用）
		url: "",	//数据请求地址
		type: "post",	//ajax请求类型
		dataType: "json",	//返回数据类型
		requestData: {},	//请求条件
		responseData: {}	//服务器响应 返回的数据
	};

	function _analysisData(data){
		if(data){
			var defaults = this, responseData = defaults.responseData = {},
				aPageAttr = ["currentPage", "totalPage", "pageNum"];
			for(var p in data){
				if(p == "data"){
					responseData.list = data[p];
				}else if(p == "page"){
					responseData.page = data.page;
					for(var i=0; i<aPageAttr.length; i++){
						var sAttr = aPageAttr[i];
						defaults[sAttr] = responseData.page[sAttr];
					}
				}else
					responseData[p] = data[p];
			}
		}
	}
})(jQuery);